//
//  ShareTool.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/21.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "ShareTool.h"
#import "WXApi.h"
#import "ShareManager.h"
#import "InviteContentLargeView.h"

@interface ShareTool()
{
    UIButton *swxq;
    UIButton *swx;
    UIButton *more;
    
    InviteContentLargeView *lView;
    UIImage *shareImg;
}
@end

@implementation ShareTool

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self buildView];
        
    }
    
    return self;
}

-(void)buildView
{
    //背景虚化
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = CGRectMake(0, 0, self.width, self.height);
    [self addSubview:effectView];
    
    NSUInteger count = 0;
    
    if ([WXApi isWXAppInstalled]) {
        
        swxq = [UIButton makeButton:^(TTButtonExtend *make) {
            make.setFrame(CGRectMake(0, 0, 60, 80));
            make.addImage([UIImage imageNamed:@"share_wxq"],UIControlStateNormal);
            make.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
            make.top = 10;
            make.addTarget(self,@selector(wxqAction));
            [self addSubview:make];
            
            UILabel *title = [UILabel makeLabel:^(TTLabelExtend *make) {
                make.setFrame(CGRectMake(0, 55, 60, 13));
                make.addText(@"朋友圈");
                make.addTextColor(FBEE_COLOR_TEXT);
                make.addTextAlignment(NSTextAlignmentCenter);
                make.addFont([UIFont systemFontOfSize:12]);
            }];
            
            [make addSubview:title];
        }];
        
        swx = [UIButton makeButton:^(TTButtonExtend *make) {
            make.setFrame(CGRectMake(0, 0, 60, 80));
            make.addImage([UIImage imageNamed:@"share_wx"],UIControlStateNormal);
            make.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
            make.top = 10;
            make.addTarget(self,@selector(wxAction));
            [self addSubview:make];
            
            UILabel *title = [UILabel makeLabel:^(TTLabelExtend *make) {
                make.setFrame(CGRectMake(0, 55, 60, 13));
                make.addText(@"微信");
                make.addTextColor(FBEE_COLOR_TEXT);
                make.addTextAlignment(NSTextAlignmentCenter);
                make.addFont([UIFont systemFontOfSize:12]);
            }];
            
            [make addSubview:title];
        }];
        count += 2;
        
    }
    
    more = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, 0, 60, 80));
        make.addImage([UIImage imageNamed:@"share_more"],UIControlStateNormal);
        make.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
        make.top = 10;
        make.addTarget(self,@selector(moreAction));
        [self addSubview:make];
        
        UILabel *title = [UILabel makeLabel:^(TTLabelExtend *make) {
            make.setFrame(CGRectMake(0, 55, 60, 13));
            make.addText(@"更多");
            make.addTextColor(FBEE_COLOR_TEXT);
            make.addTextAlignment(NSTextAlignmentCenter);
            make.addFont([UIFont systemFontOfSize:12]);
        }];
        
        [make addSubview:title];
    }];
    
    count += 1;
    
    float wd = IPHONEWIDTH/count;

    //装了微信
    if ([WXApi isWXAppInstalled])
    {
        swxq.left = (wd - 60)/2;
        swx.left = wd + (wd - 60)/2;
        more.left = 2*wd + (wd - 60)/2;
    }
    //只装了QQ
    else
    {
        more.left = (wd - 60)/2;
    }
    
}

-(void)setBeeObj:(BeeGroupListObj *)beeObj
{
    lView = [[InviteContentLargeView alloc]initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT) obj:beeObj];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        shareImg = [CommonTool getImageByView:lView];
    });
    
    _beeObj = beeObj;
}


-(void)wxqAction
{
    if (shareImg) {
        
        [[ShareManager sharedManager] shareImg:shareImg type:WxMomentsShare];
    }
}

-(void)wxAction
{
    if (shareImg) {
        
        [[ShareManager sharedManager] shareImg:shareImg type:WxShare];
    }
}

//原生分享
-(void)moreAction
{
    if (shareImg) {
        [[JumpManager sharedManager] jumpToSysShareImg:shareImg];
    }
}


@end
